<template>
  <basic-container>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <avue-crud :data="scheduleData"
                 :option="option"
                 :page="page"
                 :table-loading="tableLoading"
                 @size-change="sizeChange"
                 @current-change="currentChange"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 class="avuecrud">
        <template slot="menuRight">
          <el-button type="warning" size="small" @click="exportToDoSubjectData()">导出</el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <div class="menu-title" @click.stop="feedback(scope.row,scope.index)"
               v-if="!(scope.row.state == 3 || scope.row.state == 4 || scope.row.state == 5)">
            <i class="iconfont iconalter icon-warning"></i>
            <p class="menu-title-text">反馈</p>
          </div>
          <div class="menu-title" @click.stop="showCandidateManage(scope.row.targetId)"
               v-if="!(scope.row.state == 3 || scope.row.state == 4 || scope.row.state == 5)">
            <i class="iconfont iconalter icon-warning"></i>
            <p class="menu-title-text">转办</p>
          </div>
        </template>
      </avue-crud>
    </div>
    <candidate-manage :visible="candidateManageVisible" :after-close="closeCandidateManage" :target-id="currentTargetId"></candidate-manage>
  </basic-container>
</template>
<script>
  import {toDoTargetOpinion} from "@/api/review";
  import {exportToDoSubjectData,exportToDoSubjectDataExcel} from "@/api/opinion";
  import candidateManage from "@/components/opinion/candidateManage.vue";

  export default {
    name: "todoSubject",
    components:{
      candidateManage
    },
    data() {
      return {
        headerData: {
          title: '课题征集'
        },
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableLoading: false,
        scheduleData: [],
        option: {
          page: true,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          indexWidth:70,
          cellBtn: false,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          menuWidth: 150,
          keyId: "id",
          column: [
            {
              label: "课题征集主题",
              prop: "title",
              cell: true,
            }, {
              label: "征集主题",
              prop: "titleLike",
              search: true,
              hide: true,
              cell: true,
            }, {
              label: "发起部门",
              prop: "deptName",
              width: 120,
              cell: true
            },
            {
              label: "截止日期",
              width: 110,
              prop: "endDate",
              cell: true
            },
            {
              label: "状态",
              width: 80,
              prop: "state",
              cell: true,
              formatter: function (row, value, label, column) {
                switch (row.state) {
                  case '0':
                    return "<font color='red'>" + "催办" + "</font>";
                  case '1':
                    return "<font color='red'>" + "待反馈" + "</font>";
                  case '2':
                    return "<font color='#DCA450'>" + "补充" + "</font>";
                  case '3':
                    return "<font color='#DCA450'>" + "不采纳" + "</font>";
                  case '4':
                    return "<font color='#7EBE50'>" + "已反馈" + "</font>";
                  case '5':
                    return "<font color='#7EBE50'>" + "已采纳" + "</font>";
                }
              }
            },
          ]
        },
        titleLikeSearch:'',
        currentTargetId: null,
        candidateManageVisible: false,
      };
    },
    created() {
      window.viewDetail = this.viewDetail;
      this.getToDoList(this.page)
    },
    methods: {
      showCandidateManage(targetId){
        this.currentTargetId = targetId;
        this.candidateManageVisible = true;
      },
      closeCandidateManage(){
        this.candidateManageVisible = false;
        this.getToDoList(this.page);
      },
      getToDoList(page, params) {
        this.tableLoading = true;
        toDoTargetOpinion(Object.assign({
          current: page.currentPage,
          size: page.pageSize,
          types: '4',
          states: '0,1,2',
          descs: 'create_time'
        }, params)).then(response => {
          this.scheduleData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
          this.$emit("getTotal", response.data.data.total)
        })
      },
      /**
       * 刷新回调
       */
      searchReset() {
        this.titleLikeSearch = '';
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getToDoList(this.page,params);
      },
      //搜索回调
      searchChange (params) {
        this.page.currentPage = 1
        this.getToDoList(this.page, this.dealSearchParam(params));
      },
      // 处理搜索过滤
      dealSearchParam(params) {
        if (params.titleLike) {
          params.titleLike = params.titleLike;
        }
        this.titleLikeSearch = params.titleLike;
        return params;
      },
      currentChange(currentPage) {
        var page = this.page;
        page.currentPage = currentPage;
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getToDoList(page,params);
      },
      sizeChange(pageSize) {
        var page = this.page;
        page.pageSize = pageSize;
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getToDoList(page,params);
      },
      // 意见反馈
      feedback(row, index) {
        let src = `/subjectCollectionBack/add/${row.opinionId}`;
        this.$router.push({
          path: src,
          query: {
            type: row.type,
            targetId: row.targetId,
            toDoOrnot: '0',
            typeControlDetail: row.type,
            relateId: row.relateId,
          }
        })
      },
      exportToDoSubjectData(){
        this.$confirm("确定要导出课题征集列表吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          exportToDoSubjectDataExcel(Object.assign({
            types: '4',
            states: '0,1,2',
            descs: 'create_time'
          })).then(response => {
            let exportFileName = "课题征集列表.xlsx";
            this.download(response.data, exportFileName)
          });
        })
      },
      // 下载文件
      download(data, filename) {
        if (data == null) {

        } else {
          let url = window.URL.createObjectURL(new Blob([data]));
          let link = document.createElement('a');
          link.style.display = 'none';
          link.href = url;
          link.setAttribute('download', filename);
          document.body.appendChild(link);
          link.click()
        }
      },
    }
  };
</script>

<style lang="scss" scoped>
  /deep/ .avue-crud__menu {
    min-height: 0px;
  }
</style>

